boolean firstPress = false;
int x0 ;
int y0 ;
int x1 ;
int y1 ;

void setup() {
  size(1024, 768);
  background(255);
  noFill();
}

void mousePressed() {
  if (firstPress) {
    x1 = mouseX;
    y1 = mouseY;
    firstPress = false;
    stroke(random(1, 250), random(1, 250), random(1, 250));
  }
  else {
    x0 = mouseX;
    y0 = mouseY;
    firstPress = true;
  }
}

void draw() {
  //if (firstPress && x1!= mouseX && y1 !=  mouseY) {
  if (firstPress) {
    background(255);
//    int init = millis();
    x1 = mouseX;
    y1 = mouseY;
    stroke(random(1, 250), random(1, 250), random(1, 250));
    int r = round(sqrt(((x1-x0)*(x1-x0))+((y1-y0)*(y1-y0))));

    // f(x,y) = x^2 + y^2 - r = 0
    int h = 1-r;
    int x = x0;
    int y = y0+r;
    int dNE = 5 - 2*r;
    int dE = 3;
    while (2* (x-x0)*(x-x0) < r*r) {
      circlePoint(x, y);
      x += 1;
      y += (h>0?-1:0);
      h += (h>0?dNE:dE);
      dNE += (h>0?4:2);
      dE += 2;
    }
//    print("Took :");
//    print(millis()-init);
//    print("millis.");
//    println();
  }
}

void circlePoint(int x, int y) {
  point(x, y);
  point(x, y0-(y-y0));
  point(x0-(x-x0), y);
  point(x0-(x-x0), y0-(y-y0));

  point(x0+(y-y0), y0+(x-x0));
  point(x0+(y0-y), y0+(x-x0));
  point(x0+(y-y0), y0+(x0-x));
  point(x0+(y0-y), y0+(x0-x));
}



